import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutterhello/api/api.dart';
import 'package:flutterhello/util/host_util.dart';
import 'package:flutterhello/widgets/my_toast.dart';
import 'package:get/get.dart';

class InvDetail extends StatefulWidget {
  @override
  _InvDetailState createState() => _InvDetailState();
}

class _InvDetailState extends State<InvDetail> {
  bool _showFlag = false;
  var info = null;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    info = Get.arguments;
  }

  getImagesWidget() {
    List<Widget> imgList = [];
    info['images'].forEach((item) {
      imgList.add(
        Padding(
          padding: EdgeInsets.only(bottom: 10),
          child: Container(
            width: double.infinity,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(20),
              child: Image.network(
                HostUtil.getHost() + item,
                fit: BoxFit.cover,
              ),
            ),
          ),
        ),
      );
    });
    return imgList;
  }

  @override
  Widget build(BuildContext _context) {
    return Scaffold(
      appBar: AppBar(
        title: this._showFlag
            ? Row(
                children: [
                  CircleAvatar(
                    //头像半径
                    radius: 16,
                    //头像图片
                    backgroundImage: NetworkImage(
                        HostUtil.getHost() + info['group'][0]['favicon']),
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Container(
                    child: Text(
                      info['group'][0]['groupname'],
                      style: TextStyle(fontSize: 16),
                    ),
                  ),
                ],
              )
            : Container(),
      ),
      body: Stack(
        children: [
          Align(
            child: NotificationListener<ScrollNotification>(
              child: Padding(
                padding: EdgeInsets.only(left: 10, right: 10, bottom: 50),
                child: ListView(
                  children: [
                    SizedBox(
                      height: 10,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            CircleAvatar(
                              //头像半径
                              radius: 20,
                              //头像图片
                              backgroundImage: NetworkImage(HostUtil.getHost() +
                                  info['group'][0]['favicon']),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Container(
                              child: Text(
                                info['group'][0]['groupname'],
                                style: TextStyle(fontSize: 16),
                              ),
                            ),
                          ],
                        ),
                        Container(
                          child: Text(
                            info['createTime'],
                            style: TextStyle(color: Colors.black45),
                          ),
                        )
                      ],
                    ),
                    SizedBox(
                      height: 5,
                    ),
                    Container(
                      child: Text(
                        info['title'],
                        style: TextStyle(fontSize: 18),
                      ),
                    ),
                    Container(
                      child: Text(
                        info['content'],
                        style: TextStyle(color: Colors.black45),
                      ),
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Column(
                      children: getImagesWidget(),
                    ),
                  ],
                ),
              ),
              onNotification: (notification) {
                if (notification.metrics.extentBefore >= 50 &&
                    !this._showFlag) {
                  setState(() {
                    this._showFlag = true;
                  });
                }
                if (notification.metrics.extentBefore < 50 && this._showFlag) {
                  setState(() {
                    this._showFlag = false;
                  });
                }
                return false;
              },
            ),
            alignment: Alignment(0, -1),
          ),
          Align(
            child: Container(
              height: 50,
              width: double.infinity,
              decoration:
                  BoxDecoration(color: Color.fromARGB(255, 240, 240, 240)),
              child: Padding(
                padding: EdgeInsets.only(left: 20, right: 20),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Icon(
                      Icons.chat,
                      color: Colors.blueGrey,
                    ),
                    TextButton(
                      onPressed: () async {
                        var result = await showDialog(
                          context: _context,
                          builder: (context) {
                            return AlertDialog(
                              title: Text('提示'),
                              content: Text('确认加入吗？'),
                              actions: <Widget>[
                                TextButton(
                                  child: Text('取消'),
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  },
                                ),
                                TextButton(
                                  child: Text('确认'),
                                  onPressed: () {
                                    join(_context, info);
                                  },
                                ),
                              ],
                            );
                          },
                        );
                        print('$result');
                      },
                      child: Text('申请加入'),
                      style: ButtonStyle(
                          backgroundColor:
                              MaterialStateProperty.all(Colors.blueGrey),
                          foregroundColor:
                              MaterialStateProperty.all(Colors.white)),
                    )
                  ],
                ),
              ),
            ),
            alignment: Alignment(0, 1),
          )
        ],
      ),
    );
    ;
  }
}

join(context, info) async {
  var res = await Api.applyJoinTeam({
    "groupID": info['groupID'],
  });
  Navigator.of(context).pop();
  MyTost.showTost(res['message']);
}
